"""
@author: yjx
@file: 08.猜拳游戏.py
@time: 2023/4/14 17:25
"""
import random
import json

'''
猜拳游戏
用户可以创建房间，加入房间
加入房间之后，房间里面要等于两个人，这个房间里面的人就可以开始游戏了
两个人互相猜拳，按任意键A，B两个人随机出剪刀石头布。输的人扣100金豆。
直到金豆输完了，或者是按了q键退出

房间类
 属性：  房间id  房间里面的人 -- []
 方法：  进入房间  退出房间
 
 用户类
 属性：  用户id  金豆  属于哪个房间(1.创建房间2.进入房间)
 方法：  猜拳
 '''


# 加载用户信息
def load_user():
    try:
        with open('user.json', 'r', encoding='utf-8') as fp:
            user_dict = json.load(fp)
            print("我们现在的用户信息有：", user_dict)
            return user_dict
    except:
        user_dict = {}
        # print(user_dict)
        return user_dict


# 写入用户信息
def up_user(user_dict,name,gold):
    user_dict[name] = {"gold":gold}
    with open("user.json", "w", encoding="utf-8") as fp:
        json.dump(user_dict, fp, ensure_ascii=False, indent=4)


# 加载房间信息
def load_room():
    try:
        with open("room.json", "r", encoding="utf-8") as fp:
            room_dict = json.load(fp)
            print("我们现在的房间信息有：", room_dict)
            return room_dict
    except:
        room_dict = {}
        # print(room_dict)
        return room_dict


# 写入房间信息
def up_room(room_dict):
    with open("room.json", "w", encoding="utf-8") as fp:
        json.dump(room_dict, fp, ensure_ascii=False, indent=4)


# 定义用户类：
class User:
    # 初始化用户类，只有名字,金豆
    def __init__(self, name):
        self.name = name
        self.gold = 1000

        # int
        # print(type(self.gold))
        # int
        # print(type(self.flag))

    # 猜拳 两个用户，拳
    def guess(self):
        pass

    def register(self,name):
        pass



# 房间类
class Game:
    # # 初始化房间类
    # def room(self, room_id):
    #     self.room_id = room_id

    #     self.user_dict = {}

    # 进入房间  name,房间人数
    def get_into(self):
        print("欢迎进入房间，准备好开始游戏了吗？")


    # 创建房间  name
    def build_room(self, name):
        room_flag = [0, 0]
        room_id = input("请输入你想创建的房间id：")
        rooms = up_room(room_id)
        room_dict = {room_id: rooms}
        print(room_dict)
        print(room_dict[room_id])
        print(f"创建房间成功！")

        room_flag[0] = name
        c = input("输入1，邀请一个伙伴陪你玩游戏:")
        if c == "1":
            room_flag[1] = user1
        if len(room_flag) == 2:
            print("开始游戏!")

    def startgame(self,name):
        while 1:
            if user1.gold == 0:
                print(f"{user1.gold}金豆不足！")
                break
            if user2.gold == 0:
                print(f"{user2.gold}金豆不足！")
                break
            s = input("输入任意键开始游戏,输入q退出游戏！")
            if s == "q":
                break

            # 0 表示剪刀
            # 1 表示石头
            # 2 表示布
            # 剪刀大于布 布大于石头 石头大于剪刀
            # 0     2   2     1   1     0
            num1 = random.randint(0, 2)
            num2 = random.randint(0, 2)

            if num1 == 0 and num2 == 2 or num1 == 2 and num2 == 1 or num1 == 1 and num2 == 0:
                # user1 win
                print(name, "赢了")
                user2.gold += 100
                user1.gold -= 100
                print(f"{name}还有{user2.gold}元")
            else:
                print(user1.name, "赢了")
                user2.gold -= 100
                user1.gold += 100
                print(f"{name}还有{user2.gold}元")


# 实例化猜拳游戏
guess = Game()
print(guess)

# 加载用户信息
load_user()
# 加载房间信息
load_room()

# 创建用户，用于user1进入房间时可以直接开始游戏
user1 = User("sys")

print("欢迎来到猜拳游戏".center(40, "*"))

name = input("请输入你的名字：")
# 实例化name
user2 = User(name)
user_dict = {name: user2}
gold = user2.gold
up_user(user_dict, name, gold)
# print(user2)
# print(user2.gold)
# print(user_dict)
# print(user_dict[name])

# <__main__.User object at 0x0000023FF642BE20>

while 1:
    c1 = input("1.创建房间2.进入房间（按q退出游戏）")
    if c1 == "q":
        break
    if c1 == "1":
        guess.build_room(name)
        guess.startgame(name)
    if c1 == "2":
        guess.get_into()

        guess.startgame(name)

